from __future__ import unicode_literals
__author__ = 'Finy'

from . import Base
import time
from sqlalchemy import Column, Integer, String, \
    ForeignKey
from sqlalchemy.dialects.mysql.base import DOUBLE


class Host(Base):
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
    __tablename__ = 'host'
    __active__ = False
    hostid = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    ip = Column(String(50), nullable=False)
    port = Column(Integer, default=6379, nullable=False)
    password = Column(String(100))
    db = Column(Integer, default=0, nullable=False)
    password = Column(String(100))
    status = Column(Integer, default=1, nullable=False)
    create_time = Column(Integer, nullable=False)
    update_time = Column(Integer, nullable=False)

    @property
    def active(self):
        return self.__active__

    @active.setter
    def active(self, value):
        self.__active__ = value

    def __init__(self, **kwargs):
        '''
        Host(name='abc', ip='127.0.0.1', password='test', status=1)
        status:
            0  NORMAL
            1  DISABLE
            2  TEMPLATE
        '''
        self.name = kwargs.pop('name', '')
        self.ip = kwargs.pop('ip', '')
        self.port = kwargs.pop('port', 6379)
        self.password = kwargs.pop('password', '')
        self.status = kwargs.pop('status', 1)
        self.update_time = int(kwargs.pop('update_time', time.time()))
        self.create_time = int(kwargs.pop('create_time', time.time()))

    def __repr__(self):
        return 'Host<{name}({ip}:{port})>'.format(name=self.name, ip=self.ip,
                                                  port=self.port)


class Item(Base):
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
    __tablename__ = 'item'

    itemid = Column(Integer, primary_key=True)
    hostid = Column(Integer, ForeignKey('host.hostid'), nullable=False)
    key_ = Column(String(100), nullable=False)
    name = Column(String(100), nullable=False)
    status = Column(Integer, nullable=False)
    value_type = Column(Integer, nullable=False)
    delay = Column(Integer, nullable=False)
    units = Column(String(10), nullable=False)
    trends = Column(Integer, nullable=False)
    delta = Column(Integer, nullable=False)

    def __init__(self, **kwargs):
        '''
        Item(hostid=Host.hostid, key_="db0.keys", name="db0 all keys",
             status=1, value_type=0, delay=60, units='k', trends=365,
             delta=0)
        status:
            0  NORMAL
            1  DISABLE
            2  TEMPLATE
        value_type:
            0  NUMERIC  (history table)
            1  STRING   (history_str table)
        default:
            name  ''
            key_  ''
            status  1 (normal)
            value_type 0 (numeric)
            delay  60 (sec)
            units  ''
            trends  365 (days)
            delta  0 (not diff)
        '''
        self.hostid = kwargs.pop('hostid', 0)
        self.key_ = kwargs.pop('key_', '')
        self.name = kwargs.pop('name', '')
        self.status = kwargs.pop('status', 0)
        self.value_type = kwargs.pop('value_type', 0)
        self.delay = kwargs.pop('delay', 60)
        self.units = kwargs.pop("units", "")
        self.trends = kwargs.pop("trends", 365)
        self.delta = kwargs.pop('delta', 0)

    def __repr__(self):
        return "item:<{itemid}({key_}:{status}:{value_type}:{delay})>".format(
            itemid=self.itemid, key_=self.key_, status=self.status,
            value_type=self.value_type, delay=self.delay)


class Graph(Base):
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
    __tablename__ = 'graph'

    graphid = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    hostid = Column(Integer, ForeignKey('host.hostid'))

    def __init__(self, **kwargs):
        '''
        Each host has multiple graphics, each graphic has one or more item
        Graphs(name="cpu", hostid=1)
        '''
        self.name = kwargs.pop('name', '')
        self.hostid = kwargs.pop('hostid', 0)

    def __repr__(self):
        return "Graph:<{name}:{hostid}>".format(name=self.name,
                                                hostid=self.hostid)


class Graph_item(Base):
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
    __tablename__ = 'graph_item'

    id = Column(Integer, primary_key=True)
    graphid = Column(Integer, ForeignKey('graph.graphid'))
    itemid = Column(Integer, ForeignKey('item.itemid'))

    def __init__(self, **kwargs):
        '''
        Each graph corresponds to the item, each item can have
        multiple graphics
        Graph_item(graphid=1, itemid=2)
        '''
        self.graphid = kwargs.pop('graphid', 0)
        self.itemid = kwargs.pop('itemid', 0)

    def __repr__(self):
        return "Graph_item<{graphid}:{itemid}>".format(graphid=self.graphid,
                                                       itemid=self.itemid)


class History(Base):
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
    __tablename__ = 'history'

    id = Column(Integer, primary_key=True)
    itemid = Column(Integer, ForeignKey('item.itemid'), index=True)
    clock = Column(Integer, index=True)
    value = Column(DOUBLE)

    def __init__(self, **kwargs):
        '''
        Each item can have multiple history
        History(itemid=110, clock=1111111, value=100)
        '''
        self.itemid = kwargs.pop('itemid', 0)
        self.clock = kwargs.pop('clock', time.time())
        self.value = kwargs.pop('value', 0.0)

    def __repr__(self):
        return "History<{itemid}:{clock}:{value}>".format(
            itemid=self.itemid, clock=self.clock, value=self.value)

    def __sub__(self, h):
        return History(itemid=h.itemid, clock=h.clock,
                       value=self.value - h.value)


class History_str(Base):
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
    __tablename__ = 'history_str'

    id = Column(Integer, primary_key=True)
    itemid = Column(Integer, ForeignKey('item.itemid'), index=True)
    clock = Column(Integer, index=True)
    value = Column(String(100))

    def __init__(self, **kwargs):
        '''
        Each item can have multiple history
        History(itemid=110, clock=1111111, value='master')
        '''
        self.itemid = kwargs.pop('itemid', 0)
        self.click = kwargs.pop('clock', time.time())
        self.value = kwargs.pop('value', '')

    def __repr__(self):
        return "History_str<{itemid}:{clock}:{value}>".format(
            itemid=self.itemid, clock=self.clock, value=self.value)
